/**
 * GeneralCategory.java
 * @author: liuex
 * 2007-12-31 ����04:12:58
 *
 * note: 
 */
package lex.mtd.ui.prefs;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import org.lex.app.prefs.ResourceMap;
import org.lex.io.IO;
import org.lex.swing.prefs.CategoryAdapter;
import org.lex.swing.widgets.DirectorySelector;

import lex.mtd.Download;
import lex.mtd.utils.prefs.Keys;

public class GeneralCategory extends CategoryAdapter
{
	private JLabel directoryLabel;
	private JPanel gluePane;
	private JSpinner minTimeSpinner;
	private JLabel minTimeLabel;
	private JSpinner minBlockSpinner;
	private JLabel minBlockLabel;
	private DirectorySelector directorySelector;

	public GeneralCategory()
	{
		this.initialize();
	}

	public String getTitle()
	{
		return "General";
	}

	@Override
	public void loadConfiguration()
	{
		ResourceMap c = Download.getApplication().getContext().getResourceMap();
		this.getMinBlockSpinner().setValue( c.getInt( Keys.MINIMAL_DATA_BLOCK ) );
		this.getMinTimeSpinner().setValue( c.getInt( Keys.MINIMAL_TIME_BLOCK ) );
	}

	@Override
	public void saveConfiguration()
	{
		ResourceMap c = Download.getApplication().getContext().getResourceMap();
		c.putString( Keys.DOWNLOAD_DIRECTORY, IO.getCanonicalPath( getDirectorySelector().getDirectory() ) );
		c.putProperty( Keys.MINIMAL_DATA_BLOCK, this.getMinBlockSpinner().getValue() );
		c.putProperty( Keys.MINIMAL_TIME_BLOCK, this.getMinTimeSpinner().getValue() );
	}

	private void initialize()
	{
		GridBagLayout thisLayout = new GridBagLayout();
		this.setLayout( thisLayout );
		this.add( getDirectoryLabel(), new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
			GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
		this.add( getDirectorySelector(), new GridBagConstraints( 1, 0, 1, 1, 10.0, 0.0,
			GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 5, 0, 0 ), 0,
			0 ) );
		this.add( getGluePane(), new GridBagConstraints( 0, 3, 2, 1, 10.0, 10.0,
			GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
		this.add( getMinBlockLabel(), new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
			GridBagConstraints.BASELINE_LEADING, GridBagConstraints.NONE, new Insets( 5, 0, 0, 0 ),
			0, 0 ) );
		this.add( getMinBlockSpinner(), new GridBagConstraints( 1, 1, 1, 1, 10.0, 0.0,
			GridBagConstraints.BASELINE_LEADING, GridBagConstraints.NONE, new Insets( 5, 5, 0, 0 ),
			0, 0 ) );
		this.add( getMinTimeLabel(), new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0,
			GridBagConstraints.BASELINE_LEADING, GridBagConstraints.NONE, new Insets( 5, 0, 0, 0 ),
			0, 0 ) );
		this.add( getMinTimeSpinner(), new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0,
			GridBagConstraints.BASELINE_LEADING, GridBagConstraints.NONE, new Insets( 5, 5, 0, 0 ),
			0, 0 ) );
	}

	private JLabel getDirectoryLabel()
	{
		if( directoryLabel == null )
		{
			directoryLabel = new JLabel();
			directoryLabel.setText( "Directory:" );
		}
		return directoryLabel;
	}

	private DirectorySelector getDirectorySelector()
	{
		if( null == directorySelector )
		{
			ResourceMap c = Download.getApplication().getContext().getResourceMap();
			directorySelector = new DirectorySelector( new File( c.getString( Keys.DOWNLOAD_DIRECTORY ) ) );
			directorySelector.addDirectoryChangeListener( this );
		}
		return directorySelector;
	}

	private JPanel getGluePane()
	{
		if( gluePane == null )
		{
			gluePane = new JPanel();
		}
		return gluePane;
	}

	private JLabel getMinBlockLabel()
	{
		if( minBlockLabel == null )
		{
			minBlockLabel = new JLabel();
			minBlockLabel.setText( "Minimal Block(KB):" );
		}
		return minBlockLabel;
	}

	private JSpinner getMinBlockSpinner()
	{
		if( minBlockSpinner == null )
		{
			SpinnerNumberModel model = new SpinnerNumberModel( 20, 10, Short.MAX_VALUE, 10 );
			minBlockSpinner = new JSpinner( model );
			minBlockSpinner.addChangeListener( this );
		}
		return minBlockSpinner;
	}

	private JLabel getMinTimeLabel()
	{
		if( minTimeLabel == null )
		{
			minTimeLabel = new JLabel();
			minTimeLabel.setText( "Minimal Time(s):" );
		}
		return minTimeLabel;
	}

	private JSpinner getMinTimeSpinner()
	{
		if( minTimeSpinner == null )
		{
			SpinnerNumberModel model = new SpinnerNumberModel( 5, 5, Short.MAX_VALUE, 1 );
			minTimeSpinner = new JSpinner( model );
			minTimeSpinner.addChangeListener( this );
		}
		return minTimeSpinner;
	}
}
